<?php
/**
*  +----------------------------------------------------------------------------------------------+
*   | Explain:库存
*  +----------------------------------------------------------------------------------------------+
*   | Author: ONLY <491518132@qq.com>
*	+----------------------------------------------------------------------------------------------+
*   | Creater Time : 2013-10-31 	
*  +----------------------------------------------------------------------------------------------+
*   | Link :		http://www.phpyrb.com	     
*  +----------------------------------------------------------------------------------------------+
**/
class InventoryorderAction extends BaseAction {
	
	protected $product, $newpro;
	protected $auth;
	
	function _initialize() {
		parent::_initialize ();
		import ( 'Com.Product' );
		import ( 'Com.Newpro' );
		$this->product = new Product ();
		$this->newpro = new Newpro ();
	}
	
	/**
	 * 产品库存查询
	 */
	function index() {
		$where = array ();
		if($_REQUEST['sfield']=='all'){
			$where['product.id']=array('like',"%".$_REQUEST['keywords']."%");
			$where['product.name']=array('like',"%".$_REQUEST['keywords']."%");
			$where['_logic'] = 'OR';
		}elseif($_REQUEST['sfield']=='id'){
			$where['product.id']=$_REQUEST['keywords'];
		}elseif($_REQUEST['sfield']=='name'){
			$where['product.name']=array('like',"%".$_REQUEST['keywords']."%");
		}
		$Inventoryorder=M('InventoryOrder');
		$count = $Inventoryorder->join("left join product on product.id=inventory_order.productid")->where( $where )->count();
// 		echo $Inventoryorder->getLastSql();
		$this->assign ( "totalCount", $count );
		if ($count > 0) {
			$page = new Page ( $count, $this->listrows );
			$pages = $page->show ();
			$list=$Inventoryorder->field('inventory_order.id id,inventory_order.productid productid,inventory_order.number number,inventory_order.ctime ctime,product.name name')->join("left join product on product.id=inventory_order.productid")->where($where)->order ( 'inventory_order.id DESC' )->limit( $page->firstRow . ',' . $page->listRows)->select();

// 			$list = $Inventoryorder->where ( $where )->limit ( $page->firstRow . ',' . $page->listRows )->select ();
			$this->assign ( 'list', $list );
		}
// 		dump($list);
		$this->assign ( 'keywords', $_REQUEST['keywords'] );
		$this->assign ( 'sfield', $_REQUEST['sfield'] );
		$this->display ();
	}
	
	
	
	
	/**
	 * 处理文件上传
	 * @return string
	 */
	function _upload() {
		// $upfile=F('upfile');
		import ( "Com.UploadFile" );
		$upload = new UploadFile ();
		// 设置上传文件大小
		$upload->maxSize = 3292200;
		// 设置上传文件类型
		$upload->allowExts = explode ( ',', 'xls,xlsx,jpg' );
		// 设置附件上传目录
		$path = date ( 'Y-m' ) . '/';
		$upload->savePath = './Uploads/' . $path;
		if(! file_exists( $upload->savePath)){
			if(! mkdir ( $upload->savePath)){
				echo '创建文件夹失败！';
				exit;
			}
		}
		$path = 'Uploads/' . $path;
		// 设置需要生成缩略图，仅对图像文件有效
		// 设置上传文件规则
		$upload->saveRule = 'time';
		$upload->uploadReplace = true;
		if (! $upload->upload ()) {
			// 捕获上传异常
			$this->error ( $upload->getErrorMsg () );
		} else {
			// 取得成功上传的文件信息
			$uploadList = $upload->getUploadFileInfo ();
			$_POST ['picurl'] = $path . $uploadList [0] ['savename'];
		}
		return $_POST ['picurl'];
	}
	/**
	 * 库存导入模板页
	 * (non-PHPdoc)
	 * @see BaseAction::edit()
	 */
	function repertory(){
		
		$this->display();
	}
	/**
	 * 更新库存
	 */
	function update_repertory(){
		$repertory = array();
		$filePath = '';
		$filePath = $this->_upload ();
		$repertory = $this->newpro->read_xls($filePath);
		array_shift($repertory);
		$InventoryOrder = M('InventoryOrder');
		foreach ($repertory as $v){
			if($v['0'] > 0){
				$Inventoryo=$InventoryOrder->where(array('productid'=>$v['0']))->find ();
				if($v['5'] >= 0){
					$Inventoryo['number'] = $v['5'];
					$Inventoryo['ctime'] = time();
					if($Inventoryo['id'] > 0){
						$reid = $InventoryOrder->save($Inventoryo);
					}else{
						$Inventoryo['productid'] = $v['0'];
						$reid = $InventoryOrder->add($Inventoryo);
					}
					
				}elseif ($v['4'] >= 0){
									
					$Inventoryo['ctime'] = time();
					if($Inventoryo['id'] > 0){
						$Inventoryo['number'] = $Inventoryo['number']+$v['4'];
						$reid = $InventoryOrder->save($Inventoryo);
					}else{
						$Inventoryo['number'] = $v['4'];
						$Inventoryo['productid'] = $v['0'];
						$reid = $InventoryOrder->add($Inventoryo);
					}
				}
				
			}
		}
// 		$this->_ajm(200,'操作成功',1,U(MODULE_NAME.'/free'));
		echo '更新完成';
	}
	function delete(){
		$dao = M('InventoryOrder');
		$res=$dao->where(array("1=1"))->delete();
		if($res===FALSE){
			$this->_ajm(300,'操作错误，请重试');
		}else{
			$this->_ajm(200,'操作成功',1,U(MODULE_NAME.'/index'));
		}		
	}
}